package com.goods.controller.business;

import com.github.pagehelper.PageInfo;
import com.goods.business.service.InStockService;
import com.goods.business.service.ProductService;
import com.goods.business.service.SupplierService;
import com.goods.common.model.business.InStock;
import com.goods.common.model.business.InStockInfo;
import com.goods.common.model.business.Product;
import com.goods.common.model.business.Supplier;
import com.goods.common.response.ActiveUser;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.*;
import org.apache.catalina.security.SecurityUtil;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：Hachimen
 * @date ：Created in 2021/9/25 8:59
 * @description：入库记录
 * @modified By：
 * @version:
 */
@RestController
@RequestMapping("/business/inStock")
public class InStockWebController {

    @Autowired
    private InStockService inStockService;
    @Autowired
    private SupplierService supplierService;
    @Autowired
    private ProductService productService;
    //物资入库 分页查询
    @GetMapping("findInStockList")
    //http://www.localhost:8989/business/inStock/findInStockList?pageNum=1&pageSize=10&status=0
    // &type=1&inNum=565156165&startTime=2021-09-09+00:00:00&endTime=2021-10-20+00:00:00
    public ResponseBean findInStockList(HttpServletRequest request) {
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        String status = request.getParameter("status");
        String type = request.getParameter("type");
        String inNum = request.getParameter("inNum");
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        PageInfo<InStockVO> page = inStockService.findInStockList(pageNum,pageSize,status,type,inNum,startTime,endTime);
        //PageInfo<InStockVO> page = inStockService.findInStockList(pageNum,pageSize,inStockVO);
        Map<String, Object> data = new HashMap<>();
        data.put("total",page.getTotal());
        data.put("rows",page.getList());
        return ResponseBean.success(data);
    }

    //物资入库 明细
    //http://www.localhost:8989/business/inStock/detail/112?pageNum=1
    @GetMapping("detail/{id}")
    public ResponseBean detail(@PathVariable Long id,HttpServletRequest request){
        String pageNum = request.getParameter("pageNum");
        PageInfo<InStockItemVO> page = inStockService.detail(id,pageNum);
        InStock inStock= inStockService.findOneInStock(id);
        SupplierVO supplierVo = supplierService.edit(inStock.getSupplierId());
        Map<String, Object> data = new HashMap<>();
        data.put("itemVOS",page.getList());
        data.put("total",page.getTotal());
        data.put("supplierVO",supplierVo);
        data.put("status",inStock.getStatus());
        return ResponseBean.success(data);
    }

    //物资入库 回收站
    @PutMapping("remove/{id}")
    public ResponseBean remove(@PathVariable Long id){
        inStockService.remove(id);
        return ResponseBean.success();
    }

    //物资入库 还原
    @PutMapping("back/{id}")
    public ResponseBean back(@PathVariable Long id){
        inStockService.back(id);
        return ResponseBean.success();
    }

    //物资入库 审核
    @PutMapping("publish/{id}")
    public ResponseBean publish(@PathVariable Long id){
        inStockService.publish(id);
        return ResponseBean.success();
    }

    //物资入库 删除
    @GetMapping("delete/{id}")
    public ResponseBean delete(@PathVariable Long id){
        inStockService.delete(id);
        return ResponseBean.success();
    }

    //物资入库 添加
    @PostMapping("addIntoStock")
    public ResponseBean addIntoStock(@RequestBody InStockVO inStockVO){
        /*String address = (String)map.get("address");
        String email = (String)map.get("email");
        String name = (String)map.get("name");
        String phone = (String)map.get("phone");
        String remark = (String)map.get("remark");
        Integer sort = (Integer)map.get("sort");
        Integer type = (Integer)map.get("type");
        String contact = (String)map.get("contact");
        Integer supplierId = (Integer)map.get("supplierId");
        List<Map<String,String>> products = (List<Map<String,String>>)map.get("products");*/
        //获取用户名
        ActiveUser principal = (ActiveUser)SecurityUtils.getSubject().getPrincipal();
        String username = principal.getUser().getUsername();
        inStockService.add(inStockVO,username);
        return ResponseBean.success();
    }


}
